from config import db, admin_address, contract_name, contract_address, contract_abi, redis_service
from results import *
from utils import common_utils
from flask import jsonify

def query_bank_send_tx(address):
    """_summary_
    第一步，通过用户地址获取用户关联的交易id
    第二步，通过交易id获取交易的详情
    Args:
        address (_type_): 银行的地址

    Returns:
        _type_: _description_
    """
    # 1. 获取用户相关交易的编号
    res = common_utils.common_bc_req(admin_address, 
                                    contract_name, 
                                    'getBank', 
                                    [address], 
                                    contract_address,
                                    contract_abi)
    res_data = json.loads(res.text)


    # 2. 通过用户交易编号获取交易详情
    tx_list = []
    for i in json.loads(res_data[2]):
        tx_res = common_utils.common_bc_req(admin_address, 
                                    contract_name, 
                                    'getTransaction', 
                                    [i], 
                                    contract_address,
                                    contract_abi)
        tx_data = json.loads(tx_res.text) #拿到了智能合约中getTransaction功能的一个返回数组
        tx_dict = {
            "tid":tx_data[0],
            "sendAddress":tx_data[1],
            "receiveAddress":tx_data[2],
            "status":tx_data[3],
            "balance":tx_data[4]
        }
        tx_list.append(tx_dict)
    result = {
        "send_tx": tx_list,
    }
    return gen_result(200, result)

def query_bank_receive_tx(address):
    """_summary_
    第一步，通过用户地址获取用户关联的交易id
    第二步，通过交易id获取交易的详情
    Args:
        address (_type_): 银行的地址

    Returns:
        _type_: _description_
    """
    # 1. 获取用户相关交易的编号
    res = common_utils.common_bc_req(admin_address, 
                                    contract_name, 
                                    'getBank', 
                                    [address], 
                                    contract_address,
                                    contract_abi)
    res_data = json.loads(res.text)


    # 2. 通过用户交易编号获取交易详情
    tx_list = []
    for i in json.loads(res_data[3]):
        tx_res = common_utils.common_bc_req(admin_address, 
                                    contract_name, 
                                    'getTransaction', 
                                    [i], 
                                    contract_address,
                                    contract_abi)
        tx_data = json.loads(tx_res.text) #拿到了智能合约中getTransaction功能的一个返回数组
        tx_dict = {
            "tid":tx_data[0],
            "sendAddress":tx_data[1],
            "receiveAddress":tx_data[2],
            "status":tx_data[3],
            "balance":tx_data[4]
        }
        tx_list.append(tx_dict)
    result = {
        "receive_tx": tx_list,
    }
    return gen_result(200, result)

def freeze_account_and_send_tx(send_address, tid):
    """冻结交易

    Args:
        send_address (_type_): _description_
        tid (_type_): _description_

    Returns:
        _type_: _description_
    """
    res = common_utils.common_bc_req(admin_address, 
                                    contract_name, 
                                    'bankFreezeUserAccount', 
                                    [send_address, tid], 
                                    contract_address,
                                    contract_abi)
    if res.status_code == 200:
        return success_result
    else:
        res_json = json.loads(res.text)
        return gen_result(500, res_json['message'])
    
def confirm_tx(tid):
    """确认交易
    假设有bank1和bank2两个银行，bank1冻结了账户并提交交易，bank2可以实现交易的确认

    Args:
        tid (int): 交易编号

    Returns:
        _type_: _description_
    """
    res = common_utils.common_bc_req(admin_address, 
                                    contract_name, 
                                    'confirmTransaction', 
                                    [tid], 
                                    contract_address,
                                    contract_abi)
    if res.status_code == 200:
        return success_result
    else:
        res_json = json.loads(res.text)
        return gen_result(500, res_json['message'])
    
def reject_tx(tid):
    """拒绝交易
    假设有bank1和bank2两个银行，bank1冻结了账户并提交交易，bank2可以对提交的交易进行拒绝操作

    Args:
        tid (_type_): _description_

    Returns:
        _type_: _description_
    """
    res = common_utils.common_bc_req(admin_address, 
                                    contract_name, 
                                    'rejectTransaction', 
                                    [tid], 
                                    contract_address,
                                    contract_abi)
    if res.status_code == 200:
        return success_result
    else:
        res_json = json.loads(res.text)
        return gen_result(500, res_json['message'])